#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src/xmalloc.h"
#include "../src/hashmap.h"

/*
 * dict_t testing unit
 * compile with: gcc ../src/hashmap.o ../src/array.o ../src/xmalloc.o ../src/dict.o -lm dictest.c -o dictest
 */

#define TEST_KEY0 "First key."
#define TEST_STR0 "This is a simple test string."
#define TEST_KEY1 "Second key."
#define TEST_STR1 "This should be the value of the second key."

#define TEST_MAX 1024

int
main(int argc, char **argv)
{
  map_t *hm = hashmap_new();
  char *s = NULL, *v = NULL;
  int idx = 0;

  if (!hm) return (1);
  if (hashmap_put(hm, TEST_KEY0, (char *)TEST_STR0) != MAP_OK) return (2);
  if (hashmap_size_used(hm) != 1) return (3);
  if (hashmap_get(hm, TEST_KEY0, (void **)&s) != MAP_OK) return (4);
  if (strcmp(s, TEST_STR0) != 0) return (5);
  printf("%s\n", s);
  if (hashmap_remove(hm, TEST_KEY0) != MAP_OK) return (6);
  if (hashmap_size_used(hm) != 0) return (7);
  if (hashmap_put(hm, TEST_KEY0, (char *)TEST_STR0) != MAP_OK || hashmap_put(hm, TEST_KEY1, (char *)TEST_STR1) != MAP_OK) return (8);
  if (hashmap_size_used(hm) != 2) return (9);
  while (hashmap_iterate_all(hm, &idx, &s, (any_t *) &v)) {
    if (s && v) printf("key:%s*value:%s\n", s, v);
  }
  hashmap_free(hm);
  hm = hashmap_new();
  for (idx = 0; idx < TEST_MAX; idx++) {
    asprintf(&s, "%d", idx);
    asprintf(&v, "Value %d of %d", idx, TEST_MAX);
    hashmap_put(hm, s, v);
  }
  idx = 0;
  while (hashmap_iterate_all(hm, &idx, &s, (any_t *) &v)) {
    if (s) {
      hashmap_remove(hm, s);
      free(s);
    }
    if (v) {
      free(v);
    }
  }
  hashmap_free(hm);
  return (0);
}

